package com.bxl.netty.rpc.common.future;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * <>
 *
 * @author baixl
 * @date 2021/2/24
 */
public class CompletableFuture<T> {
    private T rtn;
    private CountDownLatch wait = new CountDownLatch(1);

    private boolean isException;
    private String msg;

    public T get() throws Exception {
        try {
            wait.await(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            isException = true;
            msg = e.getMessage();
        }
        if (isException) {
            throw new Exception(msg);
        }
        return rtn;
    }

    public void exception(String msg) {
        isException = true;
        this.msg = msg;
        wait.countDown();
    }

    public void complete(T object) {
        this.rtn = object;
        wait.countDown();
    }
}
